<template>
  <div class="setting-container">
    <el-card>
      <!-- 导航 -->
      <div slot="header">
        <el-breadcrumb>
          <el-breadcrumb-item to="/">首页</el-breadcrumb-item>
          <el-breadcrumb-item>个人设置</el-breadcrumb-item>
        </el-breadcrumb>
      </div>

      <!-- 栅格 -->
      <el-row>
        <el-col :span="12">
          <!-- 表单 -->
          <el-form label-width="120px" :model="user" :rules="rules">
            <el-form-item label="编号：">{{user.id}}</el-form-item>
            <el-form-item label="手机：">{{user.mobile}}</el-form-item>
            <el-form-item label="用户名称：" prop="name">
              <el-input v-model="user.name"></el-input>
            </el-form-item>
            <el-form-item label="用户简介：">
              <el-input
              v-model="user.intro"
                type="textarea"
                :rows="3"
              ></el-input>
            </el-form-item>
            <el-form-item label="联系邮箱：" prop="email">
              <el-input v-model="user.email"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button  type="primary" @click="userUpdateFn">保存设置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="12">
          <!-- 上传组件 action必须属性-->
          <el-upload
            class="avatar-uploader"
            action=""
            :http-request='customUploadFn'
            :show-file-list='false'
          >
            <img v-if="user.photo" class="avatar" :src='user.photo' />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <p class="edit-photo">修改头像</p>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import { userInfoAPI, userUpdateAPI, upAvatarAPI } from '@/api'
import { updateUserRules } from '@/verify/index'
export default {
  name: 'settings',
  data () {
    return {
      user: {
        id: '00',
        name: '',
        intro: '', // 简介
        photo: '',
        email: '',
        mobile: ''// 手机号
      },
      rules: updateUserRules
    }
  },
  methods: {
    async getUserInfoFn () { // 获取用户个人信息
      const [err, res] = await userInfoAPI()
      if (!err) {
        this.user = res.data.data
      }
    },
    async userUpdateFn () { // 更新用户个人信息
      const [err] = await userUpdateAPI(this.user)
      if (err) return
      this.$eventBus.$emit('namechange', this.user.name)
      // console.log(res)
      this.$message.success('信息更改成功')
      // this.getUserInfoFn()
    },
    async customUploadFn (fileObj) { // el-upload 自定义上传过程
      // 如何把文件传给后台
      // 1.文件装在FormData类型对象容器里，H5新出的
      // 2. 请求体(body/data)里 装的是formdata数据对象（请求头必须设置Content-type：multipart/formdata）
      const fd = new FormData()
      fd.append('photo', fileObj.file)
      const [err, res] = await upAvatarAPI(fd)
      if (!err) {
        this.$message.success('头像更改成功')
        this.user.photo = res.data.data.photo

        // 触发eventBus
        this.$eventBus.$emit('photourl', this.user.photo, this.user.name)
      }
    }

  },
  async created () {
    this.getUserInfoFn()
  }
}
</script>

<style scoped lang='less'>
.avatar {
  width: 178px;
  height: 178px;
  line-height: 178px;
}
.avatar-uploader {
  text-align: center;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar-uploader /deep/ .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.edit-photo {
  font-size: 12px;
  text-align: center;
}
</style>
